package com.zhi.controller;

import com.zhi.entity.*;
import com.zhi.service.HealthInfoService;
import com.zhi.service.LDCodeService;
import com.zhi.util.PageUtil;
import com.zhi.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class HealthInfoController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    HealthInfoService healthInfoService;

    @Autowired
    LDCodeService ldCodeService;

    /**
     *
     * @param model
     * @param pageUtil
     * @return
     */
    @RequestMapping("/toHealthRecord")
    public String toHealthRecord(Model model, PageUtil pageUtil,HttpSession session) {

        User sessionUser =(User)session.getAttribute("sessionUser");
        int userId = (Integer)sessionUser.getId();

        //1、初始化分页工具
        pageUtil = PageUtil.init(pageUtil);
        Pageable pageable = PageRequest.of(pageUtil.getPageIndex()-1, pageUtil.getSize());

        //2、查询分页数据
        Page<HealthInfo> pageList=healthInfoService.findAllDesc(pageable,userId);
        List<HealthInfo> healthInfos = pageList.getContent();

        //3、设置总页数
        pageUtil.setTotalPages(pageList.getTotalPages());

        //4、返回前台数据
        model.addAttribute("pageUtil", pageUtil);
        model.addAttribute("healthinfos", healthInfos);
        return "student/list";
    }

    /**
     * 跳转编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/healthinfo/toEdit")
    public String toEdit(Model model,Integer id) {
        //查询所有院系班级
        HealthInfo healthInfo=healthInfoService.getOne(id);
        List<LDCode> bodytypes = ldCodeService.findByCodeType("bodytype");

        model.addAttribute("bodytypes", bodytypes);
        model.addAttribute("healthinfo", healthInfo);

        return "student/edit";
    }

    /**
     * 编辑体质
     * @param
     * @return
     */
    @RequestMapping("/healthinfo/edit")
    public String update(HealthInfo healthInfo) {
        //studentService.edit(student);
        LDCode ldCode = ldCodeService.findByPK("bodytype",healthInfo.getBodyType());
        healthInfo.setBodyTypeDesc(ldCode.getCodeName());
        System.out.println(healthInfo);
        healthInfoService.edit(healthInfo);
        return "redirect:/toHealthRecord";
    }

    /**
     * 删除记录
     * @param id
     * @return
     */
    @RequestMapping("/healthinfo/delete")
        public String delete(Integer id) {
        //1.删除学生表数据
        healthInfoService.deleteById(id);
        return "redirect:/toHealthRecord";
    }

    /**
     * 增加
     * @param
     * @return
     */
    @RequestMapping("/healthinfo/toAdd")
    public String toAdd(Model model) {
        //查询所有体质
        List<LDCode> list = ldCodeService.findByCodeType("bodytype");
        model.addAttribute("bodytypes", list);
        return "student/add";
    }

    /**
     * 增加记录
     * @param
     * @return
     */
    @RequestMapping("/healthinfo/add")
    @ResponseBody
    public R add(HealthInfo healthInfo,HttpSession session) {
        User sessionUser =(User)session.getAttribute("sessionUser");
        int userId = (Integer)sessionUser.getId();
        //获取选择的中医体质
        LDCode ldCode = ldCodeService.findByPK("bodytype",healthInfo.getBodyType());
        healthInfo.setBodyTypeDesc(ldCode.getCodeName());
        healthInfo.setUserId(userId);
        healthInfoService.edit(healthInfo);

        return R.ok();
    }

    /**
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("toXYHealth")
    public String toXYHealth(){
        //TODO
        return "student/leaveHistory";
    }


    @RequestMapping("healthinfo/getweight")
    @ResponseBody
    public Map<String, List> getweight(HttpSession session) {
        User sessionUser =(User)session.getAttribute("sessionUser");
        Integer id = sessionUser.getId();
        Map<String, List> map = new HashMap<String, List>();
        List<HealthInfo> healthInfos = healthInfoService.findAllByUserId(id);
        //用来封装日期
        ArrayList<Date> xAxisList = new ArrayList<>();
        //用来封装体重
        ArrayList<Double> seriesWeightList = new ArrayList<>();
        //用来封装高压
        ArrayList<Double> seriesHBloodList = new ArrayList<>();
        //用来封住低压
        ArrayList<Double> seriesLBloodList = new ArrayList<>();

        for (HealthInfo healthInfo : healthInfos) {
            //添加日期
            xAxisList.add(healthInfo.getMakeDate());
            //添加体重
            seriesWeightList.add(healthInfo.getUserWeight());
            //添加高压
            seriesHBloodList.add(healthInfo.getHighBlood());
            //添加低压
            seriesLBloodList.add(healthInfo.getLowBlood());
        }
        map.put("xAxisList", xAxisList);
        map.put("seriesWeightList", seriesWeightList);
        map.put("seriesHBloodList", seriesHBloodList);
        map.put("seriesLBloodList", seriesLBloodList);

        return map;
    }
}
